package com.roncoo.pay.permission.service.impl;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.roncoo.pay.permission.entity.PmsMenu;
import com.roncoo.pay.permission.entity.PmsMenuRole;
import com.roncoo.pay.permission.repository.PmsMenuRepository;
import com.roncoo.pay.permission.repository.PmsMenuRoleRepository;
import com.roncoo.pay.permission.service.PmsMenuService;

/**
 * 菜单service接口实现
 *
 */
@Service("pmsMenuService")
public class PmsMenuServiceImpl implements PmsMenuService {

	@Autowired
	private PmsMenuRepository pmsMenuRepository;
	@Autowired
	private  PmsMenuRoleRepository pmsMenuRoleRepository;

	/**
	 * 保存菜单PmsMenuDao
	 * 
	 * @param menu
	 */
	public void savaMenu(PmsMenu menu) {
		pmsMenuRepository.save(menu);
	}

	/**
	 * 根据父菜单ID获取该菜单下的所有子孙菜单.<br/>
	 * 
	 * @param parentId
	 *            (如果为空，则为获取所有的菜单).<br/>
	 * @return menuList.
	 */
	public List<PmsMenu> getListByParent(String parentId) {
		if(StringUtils.isNotEmpty(parentId)){
			return pmsMenuRepository.findByParentId(parentId);
		}else{
			return pmsMenuRepository.findAll();
		}
	}

	/**
	 * 根据id删除菜单
	 */
	public void delete(String id) {
		pmsMenuRepository.delete(id);
	}

	/**
	 * 根据角色id串获取菜单
	 * 
	 * @param roleIdsStr
	 * @return
	 */
	public List<PmsMenu> listByRoleIds(String roleIdsStr) {
		Set<PmsMenu> menuSets = Sets.newHashSet();
		if(StringUtils.isNotEmpty(roleIdsStr)){
			String[] roleid = StringUtils.split(roleIdsStr, ",");
			for(String rid : roleid){
				if(StringUtils.isNotEmpty(rid)){
					List<PmsMenuRole> list = pmsMenuRoleRepository.findByRoleId(rid);
					for(PmsMenuRole menuRole : list){
						PmsMenu menu = pmsMenuRepository.findOne(menuRole.getMenuId());
						menuSets.add(menu);
					}
				}
			}
		}
		return Lists.newArrayList(menuSets);
	}

	/**
	 * 根据菜单ID查找菜单（可用于判断菜单下是否还有子菜单）.
	 * 
	 * @param parentId
	 *            .
	 * @return menuList.
	 */
	public List<PmsMenu> listByParentId(String parentId) {
		return pmsMenuRepository.findByParentId(parentId);
	}

	/***
	 * 根据名称和是否叶子节点查询数据
	 * 
	 * @param isLeaf
	 *            是否是叶子节点
	 * @param name
	 *            节点名称
	 * @return
	 */
	public List<PmsMenu> getMenuByNameAndIsLeaf(String menuName, String isLeaf) {
		return pmsMenuRepository.findByNameAndIsLeaf(menuName, isLeaf);
	}

	/**
	 * 根据菜单ID获取菜单.
	 * 
	 * @param pid
	 * @return
	 */
	public PmsMenu getById(String pid) {
		return pmsMenuRepository.findOne(pid);
	}

	/**
	 * 更新菜单.
	 * 
	 * @param menu
	 */
	public void update(PmsMenu menu) {
		pmsMenuRepository.save(menu);
	}

	/**
	 * 根据角色查找角色对应的菜单ID集
	 * 
	 * @param roleId
	 * @return
	 */
	public String getMenuIdsByRoleId(String roleId) {
		List<PmsMenuRole> menuList = pmsMenuRoleRepository.findByRoleId(roleId);
		StringBuffer menuIds = new StringBuffer("");
		if (menuList != null && !menuList.isEmpty()) {
			for (PmsMenuRole rm : menuList) {
				menuIds.append(rm.getMenuId()).append(",");
			}
		}
		return menuIds.toString();
	}
}
